package chen.bupt.akka.demo

import akka.actor.Actor.Receive
import akka.actor._
import akka.remote.RemoteScope
import remote.Child

/**
 * User: chenlingpeng 
 * Date: 2015/3/6 16:31.
 */
object Local extends App {

  implicit val system = ActorSystem("LocalSystem")
  val localActor = system.actorOf(Props[LocalActor], name = "LocalActor")  // the local actor
  localActor ! "START"                                                     // start the action
  system.actorOf(Props[Parent],"parent")
}

class LocalActor extends Actor {

  // create the remote actor
  val remote = context.actorSelection("akka.tcp://HelloRemoteSystem@10.1.71.169:8888/user/RemoteActor")
  var counter = 0

  def receive = {
    case "START" =>
      remote ! "Hello from the LocalActor"
    case msg: String =>
      println(s"LocalActor received message: '$msg'")
      if (counter < 5) {
        sender ! "Hello back to you"
        counter += 1
      }
  }
}

class Parent extends Actor {
  val one = AddressFromURIString("akka.tcp://HelloRemoteSystem@10.1.71.169:8888")
  val child = context.actorOf(Props[Child].withDeploy(Deploy(scope = RemoteScope(one))), "child")

  child ! "hahahahaha"
  println(child.path.address)

  override def receive: Receive = {
    case a => println("aaa"*20+a +" from "+sender().path)
  }
}

